package main

import (
	"encoding/json"
	"log"

	"gitee.com/shiwjlinux/MessageQueue/t_nats/config"
	"gitee.com/shiwjlinux/MessageQueue/t_nats/models"
	"github.com/nats-io/nats.go"
)

func consumeReviews(js nats.JetStreamContext) {
	_, err := js.Subscribe(config.SubjectNameReviewCreated, func(msg *nats.Msg) {
		//
		err := msg.Ack()
		if err != nil {
			log.Println("Unable to Ack", err)
		}

		var review models.Review
		err = json.Unmarshal(msg.Data, &review)
		if err != nil {
			log.Fatal(err)
		}

		log.Printf("Consumer -> Subject: %s - ID: %s - Author: %s - Rating: %d\n",
			msg.Subject, review.Id, review.Author, review.Rating)
	})
	if err != nil {
		log.Println("Subscribe failed")
		return
	}
}
